package com.aischool.service;

import java.util.ArrayList;
import java.util.List;

import com.aischool.params.AiClassParams;
import com.aischool.utils.RegexUtil;
import com.aischool.utils.StateEnum;
import com.aischool.utils.StringUtil;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class AiClassService extends BaseService {
	
	@Inject
	AiTeacherService aiTeacherService;
	@Inject
	AiParentService aiParentService;

	public List<Record> getAll(int org_id, int state){
		return Db.find("SELECT a.*, b.grade_name "
				+ "FROM ai_class a INNER JOIN ai_grade b ON b.grade_id = a.grade_id "
				+ "WHERE a.org_id = ? AND a.class_state = ? "
				+ "ORDER BY a.class_id DESC ", org_id, state);
	}
	
	/**
	 * 获取分页数据
	 * @param params
	 * @return
	 */
	public Page<Record> getPage(AiClassParams params){
		String sql_select = "SELECT a.*, b.grade_name  ";
		String sql_from   =	"FROM ai_class a " + 
							"INNER JOIN ai_grade b ON b.grade_id = a.grade_id ";
		String sql_where  =	"WHERE a.class_state>=0 ";
		String sql_order = "ORDER BY a.class_id DESC ";
		
		//System.err.println(JSONObject.toJSONString(params));
		
		List<Object> listObj = new ArrayList<Object>();
		if (params.getState() != StateEnum.ALL.getState()) {
			sql_where = "WHERE a.class_state = ? ";
			listObj.add(params.getState());
		}
		
		if (params.getOrgId()>0) {
			sql_where += this.getLinkWords(sql_where) + " a.org_id = ? ";
			listObj.add(params.getOrgId());
		}
		
		if (StrKit.notBlank(params.getKeywords())) {
			sql_where += this.getLinkWords(sql_where) + " a.class_name LIKE ? ";
			listObj.add("%" + params.getKeywords() + "%");
		}
		return Db.paginate(params.getPageNo(), params.getPageSize(), sql_select, sql_from + sql_where + sql_order, listObj.toArray());
	}
	
	public Record getOne(int class_id) {
		return Db.findFirst("SELECT a.* "
				+ "FROM ai_class a "
				+ " WHERE a.class_id = ?", class_id);
	}
	

	
	public boolean del(int class_id) {
		int ir = Db.delete("DELETE FROM ai_class WHERE class_id = ?", class_id);
		if (ir > 0) {
			removeCache(class_id);
		}
		return ir > 0;
	}
	
	public boolean delForOrg(int org_id, int class_id) {
		int ir = Db.delete("DELETE FROM ai_class WHERE org_id = ? AND class_id = ?", org_id, class_id);
		if (ir > 0) {
			removeCache(class_id);
		}
		return ir > 0;
	}
	
	public boolean delMore(String ids) {
		if (StrKit.isBlank(ids)) {
			return false;
		}
		if (RegexUtil.isSqlIds(ids)) {
			int ir = Db.delete("DELETE FROM ai_class a " + 
					"USING (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt " +
					"WHERE a.class_id = CAST(tt.ids AS INTEGER)", 
					StringUtil.trim(ids, ','));
			if (ir>0) {
				removeCache(ids);
			}
			return ir > 0;
		}
		
		return false;
	}
	
	public boolean delMoreForOrg(int org_id, String ids) {
		if (StrKit.isBlank(ids)) {
			return false;
		}
		if (RegexUtil.isSqlIds(ids)) {
			int ir = Db.delete("DELETE FROM ai_class a " + 
					"USING (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt " +
					"WHERE a.org_id = ? AND a.class_id = CAST(tt.ids AS INTEGER)", 
					StringUtil.trim(ids, ','), org_id);
			if (ir>0) {
				removeCache(ids);
			}
			return ir > 0;
		}
		
		return false;
	}
	
	public void removeCache(String class_ids) {
		String[] array = StringUtil.trim(class_ids, ',').split(",");
		if (array!= null && array.length>0) {
			for(int i=0; i<array.length; i++) {
				removeCache(Integer.parseInt(array[i]));
			}
		}
	}
	
	public void removeCache(int class_id) {
		aiTeacherService.removeCacheByClassId(class_id);
		aiParentService.removeCacheByClassId(class_id);
	}
}
